package com.item1024.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 阳十三
 * @email wdful165177@gmail.com
 * @blog http://www.item1024.com
 * @date 2017/8/14
 */

public class MyValidator {

    public static void main(String[] args) {
        MyValidator.isMobile("17428023106");
        MyValidator.isMail("jlimingyang13.com");
        MyValidator.isPass("123456");
    }

    /**
     * 手机号验证
     *
     * @param str 手机号
     * @return
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        try {
            p = Pattern.compile("^[1][3-8][0-9]{9}$"); // 验证手机号
            m = p.matcher(str);
            b = m.matches();
        } catch (Exception e) {
            b = false;
        }
        System.out.println("电话验证" + b);
        return b;
    }

    /**
     * @param str 固话验证
     * @return 验证通过返回true
     */
    public static boolean isPhone(String str) {
        Pattern p1 = null, p2 = null;
        Matcher m = null;
        boolean b = false;
        try {
            p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的
            p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
            if (str.length() > 9) {
                m = p1.matcher(str);
                b = m.matches();
            } else {
                m = p2.matcher(str);
                b = m.matches();
            }
        } catch (Exception e) {
            b = false;
        }
        System.out.println("固话验证" + b);
        return b;
    }

    /**
     * 验证邮箱
     *
     * @param str 邮箱
     * @return 验证通过返回true
     */
    public static boolean isMail(String str) {
        boolean b = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(str);
            b = matcher.matches();
        } catch (Exception e) {
            b = false;
        }
        System.out.println("邮箱验证:"+b);
        return b;
    }

    /**
     * 密码长度验证
     */
    public static boolean isPass(String str) {
        boolean b = false;
        if (str.length() >= 6 & str.length() <= 32) {
            b = true;
        }
        System.out.println("密码验证:"+b);
        return b;
    }
}
